package libs;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *              This class is designed to check existence of subject entity
 *              in database.
 *
 * @author      Jan Papousek
 * @version     2009-03-31
 */
public class SubjectExistence extends AbstractExistence {

    public SubjectExistence(Connection conn, Subject subject) throws SQLException {
        if (conn == null) {
            throw new NullPointerException("Argument 'conn' is null.");
        }
        if (subject == null) {
            throw new NullPointerException("Argument 'subject' is null.");
        }
        PreparedStatement preparedSt = null;
        try {
            preparedSt = conn.prepareStatement("select count(*) from subjects where code=?");
            preparedSt.setString(1, subject.getCode());
            ResultSet resultSet = preparedSt.executeQuery();
            resultSet.next();
            if(resultSet.getInt(1) == 0) {
                setExistence(true);
            }
        } finally {
            if (preparedSt != null) {
                preparedSt.close();
            }
        }
    }
    
}
